home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / DTS QT Utilities.Aug-95 / Projects & Test Apps / MovieGWorlds / MovieProc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-17  |  7.1 KB  |  236 lines  |  [TEXT/MPCC]

  1. // INCLUDES
  2. #include "MovieProc.h"
  3.  
  4.  
  5. // GLOBALS
  6. Movie                     gMovie = NULL;
  7. CWindowPtr                 gWindow = NULL;
  8.  
  9. Rect                        gMovieRect = {0, 0, 0, 0};
  10.  
  11. TimeValue                    gMovieDuration = 0L;
  12. long                        gTickCount = 0L;
  13. long                        gSampleCount = 0L;
  14. Rect                        gTimeDurationRect = { 5, 5, 10 ,110};
  15. MovieDrawingCompleteUPP    gMovieProc = NULL;
  16.  
  17. // ______________________________________________________________________
  18. //•  MAIN -- Starting point.
  19. void main(void) {
  20.     EventRecord     anEvent;
  21.     OSErr        anErr = noErr;
  22.     OSType        mediaType = VideoMediaType;
  23.     
  24.     //• Initialize the needed parts.
  25.     InitMacEnvironment(6);
  26.     InitializeQTEnvironment();
  27.     if ( InitializeMovie() != noErr ) ExitToShell();        // we had problems with the movie.
  28.  
  29.     //• Draw basic strings inside the window.
  30.     DrawFpsStats(0L); 
  31.     DrawMovieFpsStats();
  32.     DrawUsageInformation();
  33.     
  34.     //• Event loop.
  35.     for(;;) {
  36.         WaitNextEvent(everyEvent, &anEvent, 60, NULL);
  37.         
  38.         if(anEvent.what == mouseDown)
  39.             break;
  40.         
  41.         if(anEvent.what == keyDown) {        
  42.             SetGWorld(gWindow, NULL);
  43.             SetMovieGWorld(gMovie, (CGrafPtr)gWindow, NULL);
  44.             GoToBeginningOfMovie(gMovie); 
  45.             StartMovie(gMovie); 
  46.             
  47.             gTickCount = TickCount();
  48.             gSampleCount = 1L;
  49.             do {
  50.                 MoviesTask(gMovie, 0);
  51.             }
  52.             while (IsMovieDone(gMovie) == false);
  53.             
  54.             gTickCount = TickCount() - gTickCount;
  55.             DrawFpsStats(gTickCount);
  56.             QTUDrawVideoFrameAtTime(gMovie, GetMovieTime(gMovie, NULL));  // nudge one last time to make sure last frame is drawn.
  57.         }
  58.     }
  59. }
  60.  
  61. // ______________________________________________________________________
  62. //• InitMacEnvironment -- Initialize the Mac Toolbox.
  63. void InitMacEnvironment(long nMasters) {
  64.     long i;
  65.  
  66.     MaxApplZone();
  67.     
  68.     for(i = 0; i <nMasters; i++)
  69.         MoreMasters();
  70.     
  71.     InitGraf(&qd.thePort);
  72.     InitFonts();
  73.     InitWindows();
  74.     InitMenus();
  75.     FlushEvents(everyEvent, 0);
  76.     TEInit();
  77.     InitCursor();
  78.     InitDialogs(NULL);
  79. }
  80.  
  81.  
  82. // ______________________________________________________________________
  83. //• InitializeQTEnvironment -- Initialize the QuickTime movie toolbox parts.
  84. void InitializeQTEnvironment(void) {
  85.     OSErr anErr = noErr;
  86.     
  87.     if( !QTUIsQuickTimeInstalled() ) {
  88.         DebugStr("\pThe QuickTime extension is not present in this system");
  89.         ExitToShell();
  90.     }
  91.  
  92.     if( (QTUGetQTVersion() >> 16 ) < 0x200 ) {
  93.         DebugStr("\pWe need QT 2.0 or higher due to APIs used in this sample, consult the sources (exit).");
  94.         ExitToShell();
  95.     }
  96.     
  97. #if powerc    
  98.     if( !QTUIsQuickTimeCFMInstalled() ) {
  99.         DebugStr("\pThe QuickTime PowerPlug extension is not available (exit)");
  100.         ExitToShell();    
  101.     }                            
  102. #endif 
  103.     
  104.     anErr = EnterMovies(); DebugAssert(anErr == noErr);
  105.     if(anErr != noErr) {
  106.         DebugStr("\pProblems with Entermovies, returning errors (exit)");
  107.         ExitToShell();
  108.     }
  109. }
  110.  
  111.  
  112. // ______________________________________________________________________
  113. //• InitializeMovie -- Initialize needed movie parts for the offscreen handling.
  114. OSErr InitializeMovie(void) {
  115.     OSErr         anErr = noErr;
  116.     Rect             windowBounds = { kWindowYStart, kWindowXStart, kWindowHeigth, kWindowLength};
  117.     Track        firstVideoTrack = NULL;
  118.     Media        firstVideoMedia = NULL;
  119.     short        mediaPixelDepth = 0;
  120.     short        monitorPixelDepth = 0;
  121.     Rect            trackRect = {0, 0, 0, 0};
  122.     CGrafPtr        aSavedPort = NULL;
  123.     GDHandle        aSavedGDevice = NULL;
  124.     CTabHandle    colorTable = NULL;
  125.     long            seed = 0L;
  126.     
  127.     //• Create the window we will use.
  128.     gWindow = (CWindowPtr) NewCWindow(NULL, &windowBounds, "\pMovie Proc Test", true, noGrowDocProc,
  129.                                             NULL, false, 0); DebugAssert(gWindow != NULL);
  130.     SetPort((GrafPtr)gWindow);
  131.     GetGWorld(&aSavedPort, &aSavedGDevice);
  132.  
  133.     //• Get the movie.
  134.     anErr = QTUSimpleGetMovie(&gMovie); DebugAssert(anErr == noErr);
  135.     if(anErr) goto Closure;
  136.     
  137.     //• Adjust the movie box values.
  138.     GetMovieBox(gMovie, &gMovieRect); 
  139.     OffsetRect(&gMovieRect,  -gMovieRect.left,  -gMovieRect.top);
  140.     SetMovieBox(gMovie, &gMovieRect); 
  141.     AlignWindow((WindowPtr)gWindow, false,  &gMovieRect, NULL);
  142.  
  143.     //• Specify the Movie GWorld.
  144.     SetMovieGWorld(gMovie, (GWorldPtr)gWindow, NULL);
  145.     anErr = GetMoviesError(); DebugAssert(anErr == noErr); if(anErr) goto Closure;
  146.     
  147.     //• Install the movie drawing complete callback.
  148.     gMovieProc = NewMovieDrawingCompleteProc(&MyQTMovieDrawingCompleteProc);
  149.     SetMovieDrawingCompleteProc(gMovie, movieDrawingCallWhenChanged, gMovieProc, 0);
  150.     anErr = GetMoviesError(); DebugAssert(anErr == noErr); if(anErr) goto Closure;
  151.     
  152.     //• Get movie duration.
  153.     gMovieDuration = GetMovieDuration(gMovie);
  154.  
  155.     //• Update the movie so that the first frame is drawn.
  156.     anErr = QTUDrawVideoFrameAtTime(gMovie, 0L); DebugAssert(anErr == noErr);
  157.     
  158. Closure:
  159.     return anErr;
  160. }
  161.  
  162. // ______________________________________________________________________
  163. //• MyTrackTransferProc -- Callback called when movie toolbox draws to GWorld.
  164. pascal OSErr MyQTMovieDrawingCompleteProc(Movie /*theMovie*/, long /*refCon*/) {
  165.     OSErr             anErr = noErr;
  166.     PixMapHandle        offscreenPixMap = NULL;
  167.     CGrafPtr            aSavedPort = NULL;
  168.     GDHandle            aSavedGDevice = NULL;
  169.     Str255            tempString;
  170.     long                percentage = 0L;
  171.     Rect                eraseRect = { 12, kDrawTextX - 5, 24, kDrawTextX + 18};
  172.     
  173.     //• Bounce the sample code (needed for later statistics).
  174.     gSampleCount++;
  175.     
  176.     //• Draw progress bar into main screen. 
  177.     PenSize(1,1); ForeColor(blackColor); FrameRect(&gTimeDurationRect);
  178.     percentage = 100L * GetMovieTime(gMovie, NULL) / gMovieDuration;
  179.     MoveTo(5,5); ForeColor(yellowColor); PenSize(4,4); LineTo( percentage +5L, 5); 
  180.  
  181.     //• Draw percentage numbers.
  182.     EraseRect(&eraseRect);
  183.     MoveTo(kDrawTextX, 20); TextFace(bold); TextSize(9); 
  184.     NumToString(percentage, tempString);  ForeColor(redColor); DrawString(tempString); 
  185.     MoveTo(kDrawTextX + 20, 20); DrawString("\p%"); ForeColor(blackColor);
  186.     
  187.     return anErr;
  188. }
  189.  
  190.  
  191. // ______________________________________________________________________
  192. //• DrawFpsStats -- Provide and draw statistics concerning how many frames were drawn per second.
  193. void DrawFpsStats(long tickCount){
  194.     Str255     theString;
  195.     Rect        eraseArea = {kDrawValuesY-20, kDrawValuesX -20, kDrawValuesY+20, kDrawValuesX+20};
  196.  
  197.     EraseRect(&eraseArea); 
  198.     
  199.     //• Display the fps values.
  200.     MoveTo(kDrawTextX, kDrawTextY); 
  201.     TextFace(bold); TextSize(9);
  202.     DrawString("\pFrames drawn/second:");
  203.     if(tickCount != 0L) {
  204.         MoveTo(kDrawValuesX, kDrawValuesY);
  205.         NumToString( (60L * gSampleCount/tickCount), theString); 
  206.         DrawString(theString);
  207.     }
  208. }
  209.  
  210.  
  211. // ______________________________________________________________________
  212. //• DrawMovieFpsStats -- Get the statistics from the movie concerning frames per second, draw this.
  213. void DrawMovieFpsStats(void) {
  214.     Str255    theString;
  215.     long        nFrames = 0L;
  216.     long        nSeconds = 0L;
  217.     
  218.     MoveTo(kDrawTextX, kDrawTextY+40);
  219.     TextFace(bold); TextSize(9);
  220.     DrawString("\pMovie stats, fps: ");
  221.     MoveTo(kDrawValuesX, kDrawValuesY + 40);
  222.  
  223.     nFrames =  gMovieDuration / QTUGetDurationOfFirstMovieSample(gMovie, VideoMediaType);
  224.     nSeconds = gMovieDuration / GetMovieTimeScale(gMovie);
  225.     NumToString( (nFrames/nSeconds), theString);
  226.     DrawString(theString);
  227. }
  228.  
  229. // ______________________________________________________________________
  230. //• DrawUsageInformation -- Draw instructions how to use this simple program.
  231. void DrawUsageInformation(void) {
  232.     TextFace(normal); TextSize(9);
  233.     MoveTo(kDrawTextX, 200); DrawString("\pHit a key to start the movie.");
  234.     MoveTo(kDrawTextX, 220); DrawString("\pClick on the mouse to terminate program.");
  235. }
  236.